package jinyilw.swing.visuals.text;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.UISettable;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.plaf.basic.BasicEditorPaneUI;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class CenterEditorPaneUI extends BasicEditorPaneUI implements UISettable
{
	JEditorPane editorPane;
	SimpleAttributeSet attributeSet;
	StyledDocument document;

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		editorPane = (JEditorPane) c;
		editorPane.setOpaque(false);
		editorPane.setSelectionColor(RThemeColor.darkThemeColor);
		editorPane.setSelectedTextColor(RThemeColor.selectedTextColor);
		attributeSet = new SimpleAttributeSet();
		document = (StyledDocument) editorPane.getDocument();
		StyleConstants.setFontFamily(attributeSet,
				FontTools.defaultFont.getFontName());
		StyleConstants.setFontSize(attributeSet,
				FontTools.defaultFont.getSize());
		StyleConstants.setBold(attributeSet, FontTools.defaultFont.isBold());
		UITools.installUITheme(this);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		if (editorPane.getSelectionColor() != RThemeColor.darkThemeColor)
			editorPane.setSelectionColor(RThemeColor.darkThemeColor);
		Composite composite = ComponentTools.applyEnableAlpha((Graphics2D) g,
				c.isEnabled(), 1f, 0.4f);
		g.setColor(RThemeColor.colorSet.transTBGColor);
		g.fillRect(2, 2, c.getWidth() - 4, c.getHeight() - 4);
		paint(g, c);
		if (composite != null)
			((Graphics2D) g).setComposite(composite);
	}

	@Override
	protected void paintBackground(Graphics g)
	{

	}
	private void changTheme()
	{
		StyleConstants.setForeground(attributeSet,
				RThemeColor.colorSet.textColor);
		document.setCharacterAttributes(0, document.getLength(), attributeSet,
				true);
	}

	@Override
	public void lightTheme()
	{
		changTheme();
	}

	@Override
	public void darkTheme()
	{
		changTheme();
	}

	@Override
	public void blackTheme()
	{
		changTheme();
	}
}
